package com.shopping.mall.core.module.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shopping.mall.auth.annotation.UnLogin;
import com.shopping.mall.auth.utils.UserUtils;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.core.module.cart.entity.Cart;
import com.shopping.mall.core.module.order.entity.GoodOrder;
import com.shopping.mall.core.module.order.entity.Order;
import com.shopping.mall.core.module.order.entity.OrderGood;
import com.shopping.mall.core.module.order.entity.OrderPage;
import com.shopping.mall.core.module.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "Order")
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<IPage<Order>> getOrderPage(
        @RequestParam(name = "current", defaultValue = "1") Integer current,
        @RequestParam(name = "size", defaultValue = "10") Integer size,
        Order order) {
        IPage<Order> orderPage=orderService.getOrderPage(current,size,order);
        return Result.OK(orderPage);
    }

    @ApiOperation("用户分页查询")
    @GetMapping("/my/page")
    public Result<IPage<Order>> getMyOrderPage(
            @RequestParam(name = "current", defaultValue = "1") Integer current,
            @RequestParam(name = "size", defaultValue = "10") Integer size,
            Order order) {
        order.setUserId(UserUtils.getLoginUserId());
        IPage<Order> orderPage=orderService.getOrderPage(current,size,order);
        return Result.OK(orderPage);
    }


    @ApiOperation("生成订单")
    @PostMapping("/create/good")
    public Result<Order> createGoodOrder(@RequestBody Cart cart){
        return orderService.createGoodOrder(cart);
    }

    @ApiOperation("生成订单")
//    @PostMapping("/create")
    public Result<Order> createOrder(Order order){
        return orderService.createOrder(order);
    }


    @ApiOperation("修改")
    @PutMapping
    public Result<Order> update(@Validated(Order.update.class) @RequestBody Order order) {
        orderService.updateById(order);
        return Result.OK();
    }

    @ApiOperation("提交订单")
    @PostMapping("/good")
    public Result<Order> addGoodOrder(@Valid @RequestBody GoodOrder goodOrder){
        return orderService.addGoodOrder(goodOrder);
    }

    @ApiOperation("提交订单")
//    @PostMapping
    public Result<Object> add(@Validated(Order.insert.class) @RequestBody Order order) {
        return orderService.add(order);
    }

    @UnLogin
    @ApiOperation("通过id查询")
    @GetMapping("/id")
    public Result<Order> getById(@RequestParam String id){
        Order order=orderService.getById(id);
        return Result.OK(order);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public Result<List<Order>> getAll(Order order){
        List<Order> orderList=orderService.getAll(order);
        return Result.OK(orderList);
    }


    @ApiOperation("查询我的订单")
    @GetMapping("/my")
    public Result<List<Order>> getMy(Order order){
        order.setUserId(UserUtils.getLoginUserId());
        List<Order> orderList=orderService.getAll(order);
        return Result.OK(orderList);
    }

    @ApiOperation("通过id删除")
    @DeleteMapping("/id")
    public Result<Object> deleteById(@RequestParam String id){
        return orderService.deleteById(id);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    public Result<Object> batchDelete(@RequestParam List<String> ids){
        return orderService.batchDelete(ids);
    }

    @ApiOperation("支付(只需要传ID)")
//    @PostMapping("/pay")
    public Result<Object> payOrder(@RequestBody Order order){
        return orderService.payOrder(order);
    }

    @ApiOperation("取消订单")
    @DeleteMapping("/cancel")
    public Result<Object> cancelOrder(@RequestParam String orderId){
        return orderService.cancelOrder(orderId);
    }

    @ApiOperation("审核是否能够取消")
    @PostMapping("/check")
    public Result<Object> checkOrder(@RequestBody Order order){
        return orderService.checkOrder(order);
    }

    @ApiOperation("已发货")
    @PostMapping("/send")
    public Result<Object> sendOrder(@RequestBody Order order){
        return orderService.sendOrder(order);
    }

    @ApiOperation("完成订单")
    @PostMapping("/finish")
    public Result<Object> finishOrder(@RequestBody Order order){
        return orderService.finishOrder(order);
    }

    @ApiOperation("填写快递单号(输入order_good表id,deliveryBusiness，deliveryNumber)")
//    @PostMapping("/delivery")
    public Result<Object> delivery(@RequestBody OrderGood orderGood){
        return orderService.delivery(orderGood);
    }


}
